package com.eastchina.fids2focs.common.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.codehaus.xfire.transport.http.XFireServletController;

public class FIDSIIUtil {

	/**
	 * <p>获取客户端IP地址</p>
	 * @return 客户端IP
	 */
	public static String getClientIpAddr() {
		return XFireServletController.getRequest().getRemoteAddr();
	}
	
	/**
	 * <p>获取客户端MAC地址</p>
	 * @return MAC地址
	 */
	public static String getMacAddr() {
		String osName = System.getProperty("os.name");
		if (osName != null && osName.toLowerCase().indexOf("windows") != -1) {
			return getMacAddrWindows();			
		} else {
			return getMacAddrLinux();
		}

	}
	
	/**
	 * <p>获取客户端MAC地址(Server OS : Windows)</p>
	 * @return MAC地址
	 */
	public static String getMacAddrWindows() {
		return getMacAddrWindows(getClientIpAddr());
	}
	
	/**
	 * <p>获取客户端MAC地址(Server OS : Linux)</p>
	 * @return MAC地址
	 */
	public static String getMacAddrLinux() {
		return getMacAddrLinux(getClientIpAddr());
	}	
	
	/**
	 * <p>获取指定IP的MAC地址(Server OS : Windows)</p>
	 * @param ip IP地址
	 * @return MAC地址
	 */
	public static String getMacAddrWindows(String ip) {
        String str = StringUtils.EMPTY;
		String macAddress = StringUtils.EMPTY;
		
		LineNumberReader input = null;
		try {
			Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
			InputStreamReader ir = new InputStreamReader(p.getInputStream());
			input = new LineNumberReader(ir);
			
			while ((str = input.readLine()) != null) {
				if (str.indexOf("MAC Address") > 1) {
					macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
					break;
				}
			}
			
			input.close();
		} catch (IOException e) {
			return null;
		} finally {
			if (input != null) {
				try { input.close(); } catch (IOException e) { }
			}
		}
		return macAddress;    
	}
	
	/**
	 * <p>获取指定IP的MAC地址(Server OS : Linux)</p>
	 * @param ip IP地址
	 * @return MAC地址
	 */
	public static String getMacAddrLinux(String ip) {
		
		// FIXME 
		String[] cmd = {
				"/bin/sh",
				"-c",
				"ping " + ip + " -c 2 && arp -a"		
		};
		
        String str = StringUtils.EMPTY;
		String macAddress = StringUtils.EMPTY;
		
		LineNumberReader input = null;
		try {
			Process p = Runtime.getRuntime().exec(cmd);
			InputStreamReader ir = new InputStreamReader(p.getInputStream());
			input = new LineNumberReader(ir);			
			
			while ((str = input.readLine()) != null) {
				Pattern pattern = Pattern.compile("((([0-9,A-F,a-f]{1,2}:){1,5})[0-9,A-F,a-f]{1,2})");  
				Matcher matcher = pattern.matcher(str);  
				while (matcher.find()) {
					String matchStr = macAddress = matcher.group(1);

					if (str.indexOf(ip) != -1) {
						macAddress = matchStr;
						return macAddress;
					}
				}
			}			
  
		} catch(IOException e) {
			return null;
		} finally {
			if (input != null) {
				try { input.close(); } catch (IOException e) { }
			}
		}
		return macAddress;
	}
	
	/**
	 * <p>获取系统时间(UTC标准时间)</p>
	 * @return 系统时间
	 */
	public static Date getUTCSystemTime() {
		// FIXME database server's time? application server's time?
		int diff = TimeZone.getDefault().getRawOffset();
		return new Date(System.currentTimeMillis() - diff);
	}
	
	/**
	 * <p>获取系统时间</p>
	 * @return 系统时间
	 */
	public static Date getOPSSystemTime() {
		// FIXME database server's time? application server's time?
		return new Date(System.currentTimeMillis());
	}
	
	public static void main(String[] args) {
		System.out.println(getUTCSystemTime());
	}
	
}
